const puppeteer = require('puppeteer');
const MUSIC_URL = 'https://music.163.com/#/discover/playlist';
const main = async () => {
    const ua = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36";

    const browser = await puppeteer.launch({
        headless: false,
        args: ['--no-sandbox', ua]
    })
    const page = await browser.newPage();
    await page.goto(MUSIC_URL)
    // 获取歌单的iframe
    let iframe = await page.frames().find(f => f.name() === 'contentFrame');
    // 获取歌单
    const result = await iframe.evaluate(() => {
        // 获取所有元素
        const elements = document.querySelectorAll('#m-pl-container > li');
        // 创建数组
        let res = [];
        for (let ele of elements) {
            console.log(ele)
            const image = ele.querySelector('.j-flag').getAttribute('src');
            const name = ele.querySelector('.tit').innerText;
            const count = ele.querySelector('.nb').innerText;
            const author = ele.querySelector('.nm').innerText;
            res.push({
                image,
                name,
                count,
                author

            })
        }
        return res;
    })
    console.log(result)

}
main()